package reduce.fz.com.reduce.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;

/**
 * Created by qingshengzheng on 15/12/21.
 */
public class TextListView extends ListView {
    private TextBaseAdapter textAdapter;

    public void init(List<String> datas,ItemClickListener listener)
    {
        textAdapter=new TextBaseAdapter(this.getContext(),datas, listener);
        this.setAdapter(textAdapter);
    }






    public static class TextBaseAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater layoutInflater;
        private ItemClickListener listener;

        public TextBaseAdapter(Context context,List<String> datas,ItemClickListener listener) {
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
            this.datas=datas;
            this.listener=listener;

        }

        public List<String> datas = new ArrayList<String>();

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }




        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = layoutInflater.inflate(R.layout.base_text_view_item, parent, true);
            TextView textView = (TextView) view.findViewById(R.id.tv_text);
            textView.setText(datas.get(position));
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.itemClick(datas.get(position));
                }
            });
            return view;
        }
    }

    public static  interface ItemClickListener{
        public void itemClick(String item);
    }

    public TextListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}
